package com.jumper.mydemo.statics;

import java.util.Map;

import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.core.ResolvableType;
import org.springframework.stereotype.Service;

import com.jumper.mydemo.my.MyConfiguration;
import com.jumper.mydemo.my.MyService;

public class BeanFactoryListableDemo {
    public static void run() {
        @SuppressWarnings("resource")
        ListableBeanFactory beanFactory = new AnnotationConfigApplicationContext(MyConfiguration.class).getBeanFactory();

        boolean containsBeanDefinition = beanFactory.containsBeanDefinition("myService");
        System.out.println("判断是否包含指定名称的Bean定义:" + containsBeanDefinition);

        int beanDefinitionCount = beanFactory.getBeanDefinitionCount();
        System.out.println("获取工厂中所有Bean定义的数量:" + beanDefinitionCount);

        String[] beanDefinitionNames = beanFactory.getBeanDefinitionNames();
        System.out.println("获取工厂中所有Bean定义的名称:" + String.join(",", beanDefinitionNames));

        ObjectProvider<MyService> objectProvider = beanFactory.getBeanProvider(MyService.class, true);
        System.out.println("获取Bean的ObjectProvider:" + objectProvider.getObject());

        String[] beanNamesForType = beanFactory.getBeanNamesForType(ResolvableType.forClass(MyService.class));
        System.out.println("根据类型获取Bean名称:" + String.join(",", beanNamesForType));

        String[] beanNamesForAnnotation = beanFactory.getBeanNamesForAnnotation(Service.class);
        System.out.println("根据注解获取Bean名称:" + String.join(",", beanNamesForAnnotation));

        Map<String, Object> beansWithAnnotation = beanFactory.getBeansWithAnnotation(Service.class);
        System.out.println("根据注解获取所有Bean实例:" + beansWithAnnotation);

        Service annotation = beanFactory.findAnnotationOnBean("myService", Service.class);
        System.out.println("指定Bean上查找指定类型的注解:" + annotation);
    }
}
